import os
import django
import grpc
import time
from concurrent import futures

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.common")
django.setup(set_prefix=False)

from django.conf import settings


def register_app(server):

    from lib.services import NewsService
    from apps.news import news_pb2_grpc
    news_pb2_grpc.add_NewsServicer_to_server(NewsService(), server)


def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    register_app(server)
    server.add_insecure_port('[::]:'+ settings._PORT)
    print("server started")
    server.start()
    try:
        while True:
            time.sleep(settings._ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        server.stop(0)


if __name__ == '__main__':
    serve()